package com.mantru.iotpot.setting.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.DateClass.Calibration_date;
import com.mantru.iotpot.R;
import com.mantru.iotpot.setting.calibrateActivity;

import java.util.List;

public class SeasoningCalibrationAdapter extends RecyclerView.Adapter<SeasoningCalibrationAdapter.ViewHolder> {
    private List<Calibration_date> calibrationDates;
    private ApplicationClass ac;
    private Context context;
    @NonNull
    @Override
    public SeasoningCalibrationAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_seasoning_calibration, parent, false);
        return new SeasoningCalibrationAdapter.ViewHolder(view);
    }

    public SeasoningCalibrationAdapter(List<Calibration_date> calibrationDates, ApplicationClass ac,Context context) {
        this.calibrationDates = calibrationDates;
        this.ac = ac;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(@NonNull SeasoningCalibrationAdapter.ViewHolder holder, int position) {
        Calibration_date calibrationDate = calibrationDates.get(position);
        holder.name.setText(calibrationDate.getNozzle_name());
        if(calibrationDate.getIs_need_calibration().equals("1")){//需要校准
            holder.need.setVisibility(View.VISIBLE);
        }else if (calibrationDate.getIs_need_calibration().equals("0")){//不需要校准
            holder.need.setVisibility(View.INVISIBLE);
        }

        holder.button1.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ac.Calibration_Control_Nozzle(Integer.parseInt(calibrationDate.getNozzle_id()), 1);
                Log.d("Calibration_Control_Nozzle", "pressed");
                holder.button1.setPressed(true);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                ac.Calibration_Control_Nozzle(Integer.parseInt(calibrationDate.getNozzle_id()), 0);
                holder.button1.setPressed(false);
                Log.d("Calibration_Control_Nozzle", "released");
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                // 忽略移动事件，不执行任何操作
                return true;
            }
            return false;
        });

        holder.button2.setOnClickListener(v -> {//去校准
            final int p = position;
            showCalibrateDialog(Integer.parseInt(calibrationDate.getNozzle_id()),p);
        });
    }

    @Override
    public int getItemCount() {
        return calibrationDates.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView name;
        private final Button need;
        private final Button button1;
        private final Button button2;

        public ViewHolder(View view){
            super(view);
            name = view.findViewById(R.id.adapter_seasoning_calibration_name);
            need = view.findViewById(R.id.adapter_seasoning_calibration_need);
            button1 = view.findViewById(R.id.adapter_seasoning_calibration_button1);
            button2 = view.findViewById(R.id.adapter_seasoning_calibration_button2);
        }
    }
    // 显示校准对话框的方法
    private void showCalibrateDialog(final int nozzleId,final int p) {
        // 创建对话框
        Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.dailog_calibrate); // 加载对话框布局
        dialog.setTitle("校准");
        // 获取对话框中的控件
        Button confirmButton = dialog.findViewById(R.id.dailog_calibrate_star); // 假设有一个确认按钮
        ImageView cancelButton = dialog.findViewById(R.id.dailog_calibrate_cencle); // 假设有一个取消按钮
        // 设置按钮的点击事件
        confirmButton.setOnClickListener(v -> {
            // 点击开始校准，跳转到 calibrateActivity 并传递 nozzleId
            Intent intent = new Intent(context, calibrateActivity.class);
            intent.putExtra("nozzle_name",calibrationDates.get(p).getNozzle_name());
            ac.Calibration_Control_Begin(nozzleId);
            context.startActivity(intent);
            dialog.dismiss(); // 关闭对话框
        });
        cancelButton.setOnClickListener(v -> {
            // 点击取消，关闭对话框
            dialog.dismiss(); // 关闭对话框
        });
        dialog.show(); // 显示对话框
        // 关键代码：设置窗口背景透明
        if (dialog.getWindow() != null) {
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        // 关键代码：设置点击外部不可取消
        dialog.setCanceledOnTouchOutside(false);
    }
}
